package com.fwtai.api.controller;

import com.fwtai.config.ConfigFile;
import com.fwtai.entity.ReqPage;
import com.fwtai.service.api.ApiBuyService;
import com.fwtai.service.api.ApiMallService;
import com.fwtai.tool.ToolClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 微信或积分支付
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2021-12-17 14:25
 * @QQ号码 444141300
 * @Email service@dwlai.com
 * @官网 http://www.fwtai.com
*/
@Api(tags = "积分商城,token选填",description = "积分商城")
@RestController
@RequestMapping(ConfigFile.guest_v10 + "mall")
public class MallController{

  private final ApiBuyService apiBuyService;

  private final ApiMallService apiMallService;

  public MallController(final ApiBuyService apiBuyService,final ApiMallService apiMallService){
    this.apiBuyService = apiBuyService;
    this.apiMallService = apiMallService;
  }

  @ApiOperation(value = "购买积分商城的商品(不需要自动确认收货,但是微信支付订单要推送到微信服务器发货信息),token必填", notes = "提交金额并调起支付功能,下单前得先调用接口 /api/guest/v1.0/wechat/getInfo")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "openId", value = "当前登录绑定的微信openId", dataType = "String", paramType = "query",required = true),
    @ApiImplicitParam(name = "addressId", value = "用户的收货地址", dataType = "String", paramType = "query",required = true),
    @ApiImplicitParam(name = "mode", value = "购买类型（1微信支付|2积分支付）", dataType = "int", paramType = "query",required = true),
    @ApiImplicitParam(name = "orderInfo", value = "商品参数,格式:[{\"spec_id(商品规格的kid)\":\"416346836043833349\",\"number\":1},{\"spec_id(商品规格的kid)\":\"440026041285615622\",\"number\":2}]", dataType = "String", paramType = "query",required = true)
  })
  @PostMapping("/addOrder")
  public void addOrder(final HttpServletRequest request,final String mode,final HttpServletResponse response){
    ToolClient.responseJson(apiBuyService.addOrder(request,mode),response);
  }

  /**获取积分商城的订单分页数据*/
  @ApiOperation(value = "获取积分商城的订单分页数据,默认仅含数据不含总记录数总条数", notes = "其参数是搜索过滤条件;排序的key为column值为返回的字段，排序关键字的key为order,其值可选正序ASC|倒序DESC<br/>注意调用端需要判断每次请求返回的条数是否小于每页大小或返回的code=201而停止下一次请求<br/><span class='danger'>前端需要处理日期时间格式为时间戳</span>")
  @GetMapping("/listDataPage")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "status", value = "提货状态(1待确认收货;2已完成;3待发货;不传则是全部)", dataType = "String", paramType = "query", required = false)
  })
  public void listDataPage(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiBuyService.listDataPage(request),response);
  }

  @ApiOperation(value = "获取积分商城的订单详情,token必填", notes = "获取积分商城的订单详情")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "out_trade_no", value = "订单编号", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "id", value = "被查看的kid", dataType = "long", paramType = "query", required = true)
  })
  @GetMapping("/queryOrderInfo")
  public void queryOrderInfo(final Long id,final Long out_trade_no,final HttpServletResponse response){
    ToolClient.responseJson(apiBuyService.queryOrderInfo(id,out_trade_no),response);
  }

  @ApiOperation(value = "用户确认收货(用户已取货)", notes = "用户确认收货(用户已取货)")
  @PostMapping("/updateCargo")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "out_trade_no", value = "订单编号", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "type", value = "订单类型(1是微信支付订单,2是积分进货订单)", dataType = "int", paramType = "query", required = true)
  })
  public void updateCargo(final String out_trade_no,final String type,final HttpServletResponse response){
    ToolClient.responseJson(apiBuyService.updateCargo(type,out_trade_no),response);
  }

  @PostMapping("/addNotify")
  public void addNotify(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiBuyService.addNotify(request),response);
  }

  @ApiOperation(value = "获取积分商城的商品列表分页数据,默认仅含数据不含总记录数总条数", notes = "其参数是搜索过滤条件;排序的key为column值为返回的字段，排序关键字的key为order,其值可选正序ASC|倒序DESC<br/>注意调用端需要判断每次请求返回的条数是否小于每页大小或返回的code=201而停止下一次请求<br/><span class='danger'>前端需要处理日期时间格式为时间戳</span>")
  @GetMapping("/listMallGoods")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "category", value = "商品类型id", dataType = "String", paramType = "query", required = false),
    @ApiImplicitParam(name = "name", value = "商品名称", dataType = "String", paramType = "query", required = false)
  })
  public void listMallGoods(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiMallService.listMallGoods(request),response);
  }

  @ApiOperation(value = "获取积分商城的商品详情")
  @GetMapping("/getGoodsInfo")
  @ApiImplicitParam(name = "kid", value = "积分商城的商品kid", dataType = "String", paramType = "query", required = true)
  public void getGoodsInfo(final Long kid,final HttpServletResponse response){
    ToolClient.responseJson(apiMallService.getGoodsInfo(kid),response);
  }

  @ApiOperation(value = "获取积分商城的商品分类",notes = "获取积分商城的商品分类")
  @GetMapping("/getGoodsCategory")
  public void getGoodsCategory(final HttpServletResponse response){
    ToolClient.responseJson(apiMallService.getGoodsCategory(),response);
  }
}